<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="学期" prop="sem_id">
        <el-select
          v-model="queryParams.sem_id"
          placeholder="请选择"
          clearable
          filterable
        >
          <el-option
            v-for="dict in semesterOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table ref="table" v-loading="loading" :data="scheduleList" key="rk">
      <el-table-column label="节次" align="center" prop="name" />
      <el-table-column
        :label="w.label"
        align="center"
        v-for="(w, i) in weekOptions"
        :key="i"
      >
        <template slot-scope="scope">
          <div v-if="scope.row['w' + w.value]">            
            <div>{{ scope.row["w" + w.value].class_name }}</div>
            <div>{{ scope.row["w" + w.value].course_name }}</div>
            <div>{{ scope.row["w" + w.value].room_name }}</div>
          </div>         
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDs } from "@/api/openapi";

export default {
  name: "ClassSchedule",
  dicts: ["ext_week"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      scheduleList: [],
      semId: null,
      semesterOptions: [],
      classOptions: [],
      courseOptions: [],
      sectionOptions: [],
      weekOptions: [],
      teacherOptions: [],
      roomOptions: [],
      // 查询参数
      queryParams: {
        sem_id: null,
        teacher_id: "@userId()",
        "@table": "teach_schedule",
        "@exp": {},
      },
      pager: {
        page: 1,
        limit: 1000,
        order: "id desc",
      }
    };
  },
  created() {
    this.initOptions();
  },
  methods: {
    /** 查询课程列表 */
    getList() {
      if (!this.queryParams.sem_id) {
        this.loading = false;
        return;
      }
      this.loading = true;
      let params = { t: { ...this.queryParams } };
      params["c"] = {
        "@table": "base_course",
        "@join": "c.id = t.course_id",
        "@column": "name:course_name",
      };
      params["cc"] = {
        "@table": "base_class",
        "@join": "cc.id = t.class_id",
        "@column": "name:class_name",
      };
      params["r"] = {
        "@table": "base_room",
        "@join": "LEFT r.id = t.room_id",
        "@column": "name:room_name",
      };
      params["@pager"] = this.pager;
      apiGet(params).then((response) => {
        if (response.data.rows == null || response.data.rows.length == 0) {
          this.scheduleList = this.initScheculeTable();
        } else {
          this.initSchecule(response.data.rows);
        }
        this.loading = false;
      });
    },
    initSchecule(rows) {
      let scheduleList = this.initScheculeTable();
      for (let i = 0; i < rows.length; i++) {
        let s = rows[i];
        for (let j = 0; j < scheduleList.length; j++) {
          if (scheduleList[j].id == s.section_id) {
            scheduleList[j]["w" + s.week] = s;
          }
        }
      }
      this.scheduleList = scheduleList;
    },
    initOptions() {
      let ps = [];

      ps.push(
        new Promise((resolve, reject) => {
          apiGet({
            "@table": "base_section",
            status: "1",
            "@pager": { page: 1, limit: 100, order: "num ASC" },
          }).then((response) => {
            this.sectionOptions = response.data.rows;
            resolve();
          });
        })
      );

      ps.push(
        new Promise((resolve, reject) => {
          apiGet({
            "@table": "base_semester",
            status: "1",
            "@pager": { page: 1, limit: 100 },
          }).then((response) => {
            this.semesterOptions = response.data.rows;
            for (let i = 0; i < this.semesterOptions.length; i++) {
              if (this.semesterOptions[i]["is_current"] == "Y") {
                this.queryParams.sem_id = this.semesterOptions[i].id;
              }
            }
            resolve();
          });
        })
      );
      ps.push(
        new Promise((resolve, reject) => {
          apiDs("sys_dict_select", { dict: "ext_week" }).then((res) => {
            let weekOptions = [];
            for (let i = 0; i < res.data.length; i++) {
              if (res.data[i].status == "0") {
                weekOptions.push(res.data[i]);
              }
            }
            this.weekOptions = weekOptions;
            resolve();
          });
        })
      );
      Promise.all(ps).then((res) => {
        this.getList();
      });
    },
    initScheculeTable() {
      let scheduleOptions = [];
      for (let i = 0; i < this.sectionOptions.length; i++) {
        let s = {...this.sectionOptions[i]};
        s['rk'] = this.queryParams.sem_id+"_"+this.queryParams.class_id+"_"+s.id;
        for (let j = 0; j < this.weekOptions.length; j++) {
          s["w" + this.weekOptions[j].value] = null;
        }
        scheduleOptions.push(s);
      }
      return scheduleOptions;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    }
  },
};
</script>
